package org.rslai.tcedit.dao.hibernate;

import java.util.List;
import org.rslai.tcedit.domain.Page;
import org.rslai.tcedit.domain.User;
import org.rslai.tcedit.dao.UserDao;
import org.rslai.tcedit.exception.ApplicationException;
import org.springframework.security.userdetails.UsernameNotFoundException;

/**
 * UserDao的实现类
 * 
 * @author rslai
 * 
 * @spring.bean id="userDao"
 */
public class UserDaoImpl extends GenericHibernateDao<User> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	/**
	 * 创建一个 user 信息。覆盖 GenericHibernateDao 中的 create 方法。
	 * 
	 * @param user user对象
	 */
	public void create(User user) {
		User tmpUser = (User) queryForObject(
				"from User as a where a.username=?",
				new Object[] { user.getUsername() }
		);
		if (tmpUser != null) {
			throw new ApplicationException("您输入的“用户名”已存在，请重新输入。");
		}

		hibernateTemplate.save(user);
	}
	
	/**
	 * 更新一个 user 信息。覆盖 GenericHibernateDao 中的 create 方法。
	 * 
	 * @param user user 对象
	 */
	public void update(User user) {
		User tmpUser = (User) queryForObject(
				"from User as a where a.username=? and a.id!=?",
				new Object[] { user.getUsername(), user.getId() }
		);
		if (tmpUser != null) {
			throw new ApplicationException("您输入的“用户名”已存在，请重新输入。");
		}

		hibernateTemplate.update(user);
	}
	
    public User queryUser(String username) {
    	User user = (User) queryForObject(
                "from User as a where a.username=?",
                new Object[] {username}
        );
        if(user!=null)
            return user;
        throw new UsernameNotFoundException("用户名不存在");
    }

	private static final String QUERY_USER_ASC = "from User as a order by a.realName";
	private static final String QUERY_USER_DESC = "from User as a order by a.realName desc";

	@SuppressWarnings("unchecked")
	public List<User> query(boolean asc) {
		return queryForList(
				asc ? QUERY_USER_ASC : QUERY_USER_DESC,
				null);
	}

    private static final String QUERY_USER_NAME_ASC = "from User as a where a.username LIKE ? AND a.realName LIKE ? AND a.email LIKE ? order by a.username";
    private static final String QUERY_USER_NAME_DESC = "from User as a where a.username LIKE ? AND a.realName LIKE ? AND a.email LIKE ? order by a.username desc";

    @SuppressWarnings("unchecked")
	public List<User> query(Page page, String username, String realName, String email, boolean asc) {
		return queryForList(
				"select count(*) from User as a where a.username LIKE ? AND a.realName LIKE ? AND a.email LIKE ?",
				asc ? QUERY_USER_NAME_ASC : QUERY_USER_NAME_DESC, 
				new Object[] { username, realName, email },
				page
		);
	}

	public void changePassword(String username, String oldPassword, String newPassword) {
		if (!oldPassword.matches("[a-f0-9]{32}"))
			throw new ApplicationException("无效的口令");
		if (!newPassword.matches("[a-f0-9]{32}"))
			throw new ApplicationException("无效的口令");
		int rows = executeUpdate(
				"update User as a set a.password=? where a.username=? and a.password=?",
				new Object[] { newPassword, username, oldPassword });
		if (rows == 0)
			throw new ApplicationException("旧口令输入错误!");
	}

}
